using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using MultiAgentsServer.Scripts.Database;
using System.Security.Claims;

namespace MultiAgentsServer.Pages;

public class IndexModel(DataBaseOperationService dataBaseOperationService) : PageModel
{
    private readonly DataBaseOperationService _dataBaseOperationService = dataBaseOperationService;

    public void OnGet() { }

    public IActionResult OnPost(string username, string password)
    {
        bool loginSuccess = _dataBaseOperationService.CheckManagerLogin(username, password);

        if (loginSuccess)
        {
            var claims = new List<Claim>
            {
                new Claim(ClaimTypes.Name, username)
            };

            var claimsIdentity = new ClaimsIdentity(claims, "CookieAuth");
            var claimsPrincipal = new ClaimsPrincipal(claimsIdentity);

            HttpContext.SignInAsync("CookieAuth", claimsPrincipal);

            return RedirectToPage("InviteManager");
        }
        else
        {
            return Page();
        }
    }
}
